from global_names import *
CONTOUR_TYPE = cv2.RETR_EXTERNAL #RETR_EXTERNAL RETR_LIST
CONTOUR_APPROX = cv2.CHAIN_APPROX_NONE #NONE  SIMPLE  TC89_L1
from contours import inside_focus_area

def empty_frame(frame_shape=None,height=None,width=None):
	if frame_shape:
		return np.zeros(frame_shape)
	else:
		return np.zeros((height,width))

class Frame():
	"""
	The Frame class defines a single video frame.
	Upon initialization, it loads the image located at the specified frame_path, and crops it to the size specified in the video_params.
	Call on the Frame's image, height, and width.
	"""
	def __init__(self,frame_path, video_params):
		image, height, width = self.load_image(frame_path)
		focus_area = video_params['focus_area']
		self.image, self.height, self.width = self.crop(image, focus_area)
	def get_image(self):
		return self.image.copy()
	def load_image(self, path_name):
		try:
			image = cv2.imread(path_name, 0) #0 for B&W
			height = np.size(image, axis = 0)
			width = np.size(image, axis = 1)
			return (image, height, width)
		except:
			print "Could not load image file: %s"%path_name
			exit()
	def crop(self, img, focus_area=None):
		if focus_area == None:
			img_cropped = img
		elif type(focus_area[0]) == type(1): #regularly oriented rectangle
			img_cropped = img[focus_area[Y1]:focus_area[Y2]]
			img_cropped = img_cropped[:,focus_area[X1]:focus_area[X2]]
		elif type(focus_area[0]) == type([1]): #four corners, possibly tilted region
			#crop to bounding box:
			x1 = min([corner[X] for corner in focus_area])
			x2 = max([corner[X] for corner in focus_area])
			y1 = min([corner[Y] for corner in focus_area])
			y2 = max([corner[Y] for corner in focus_area])
			img_cropped = img[y1:y2]
			img_cropped = img_cropped[:,x1:x2]
			#white out remainder:
			# for yidx, y in enumerate(img_cropped):
			# 				for xidx, x in enumerate(y):
			# 					if not inside_focus_area(point=[xidx,yidx], focus_area=focus_area):
			# 						img_cropped[yidx,xidx] = 100
		return (img_cropped, np.size(img_cropped, axis=0), np.size(img_cropped, axis=1))
		
class ContoursFrame():
	def __init__(self, frame, params):
		self.image = frame.get_image()
		self.image_original = self.get_image()
		self.height = frame.height
		self.width = frame.width
		
		brightness = params['brightness']
		contrast = params['contrast']
		canny_thresh1 = params['canny_thresh1']
		canny_thresh2 = params['canny_thresh2']
		kern_size = params['kern_size']
		
		self.adjust_contrast(contrast)
		self.adjust_brightness(brightness)
		self.find_edges(canny_thresh1, canny_thresh2)
		self.dilate(kern_size)
		self.contours = self.find_contours()
	def get_image(self):
		return self.image.copy()
	def adjust_contrast(self,contrast):
		self.image = self.image*contrast
	def adjust_brightness(self,brightness):
		self.image = self.image + brightness
	def find_edges(self, canny_thresh1, canny_thresh2):
		self.image = cv2.Canny(self.get_image(), canny_thresh1, canny_thresh2) #find edges
	def dilate(self, kern_size):
		def kernel(size=3):
			dims = (size,size)
			return cv2.getStructuringElement(cv2.MORPH_RECT,dims) #RECT ELLIPSE CROSS
		if kern_size>0:
			self.image = cv2.dilate(self.get_image(), kernel(size=kern_size))
	def find_contours(self):
		contours, hier = cv2.findContours(self.get_image(), CONTOUR_TYPE, CONTOUR_APPROX) #find contours
		return contours
		
	